package com.tanhua.manager.listener;

import com.tanhua.api.mongo.PublishApi;
import com.tanhua.autoconfig.template.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "tanhua-publish", consumerGroup = "tanhua-publish-consumer")
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String publishId) {
        System.out.println("动态审核：" + publishId);

        // 1、查询动态详情
        Publish publish = publishApi.findById(publishId);

        // 2、文本和图片审核
        if (publish.getState() == 0) { // 校验
            // 文本
            boolean textCheck = huaWeiUGCTemplate.textContentCheck(publish.getTextContent());
            // 图片
            boolean imageCheck = huaWeiUGCTemplate.imageContentCheck(publish.getMedias().toArray(new String[]{}));

            // 要求二个都成功了，状态为1
            Integer state = 2;
            if (textCheck && imageCheck) {
                state = 1;
            }

            // 更新动态状态码
            publishApi.updateState(publishId, state);
        }
    }
}
